package com.hspedu.outputstream;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/25
 * @time 2:38
 */
public class ObjectOutputStream_ {
    public static void main(String[] args)  {

    }

    /**
     * 序列化
     * @throws Exception
     */
    @Test
    public void serialize()throws Exception{
        String fileName = "D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\news6.txt";

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));

        //序列化数据
        oos.writeInt(100);
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeDouble(9.5);
        oos.writeUTF("hspedu");

        oos.writeObject(new Dog("小黄",3));

        oos.close();
        System.out.println("数据保存完毕（序列化形式）");
    }

    /**
     * 反序列化
     * @throws Exception
     */
    @Test
    public void deserialization() throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\BaiduNetdiskDownload\\韩顺平2021基础学Java\\temp\\news6.txt"));
        System.out.println(ois.readInt());

        System.out.println(ois.readBoolean());

        System.out.println(ois.readChar());

        System.out.println(ois.readDouble());

        System.out.println(ois.readUTF());

        Object o = ois.readObject();
        System.out.println(o.getClass());
        System.out.println(o);

        ois.close();
        System.out.println("反序列化读取完成");
    }
}

